<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * Please check this document before testing.
 * 
 *
*/

class Vote extends CI_Model
{
	protected $table = 'vote';
	protected $citizen_table = 'citizen';
	protected $incity_table = 'incity';
	
	public function newVote($from, $to, $id_city, $vote = 1) 
	{ 
		
		$results = $this->db->select( 'id_city, vote' )
						->from( $this->table )
						->where ('id_from', $from )
						->where( 'id_to', $to )
						->get()
						->result();
		
		if ( empty( $results ) ) 
		{ //No vote sent
		
			$this->db->set( 'id_from', $from )
					 ->set( 'id_to', $to )
					 ->set( 'id_city', $id_city )
					 ->set( 'vote', $vote )
					 ->insert( $this->table );
					 
			return $this->recalc( $to, 0, 0, $id_city, $vote );
		
		} 
		else // ( count( $results ) == 1 ) 
		{ 
		
			$this->db->set('id_city', $id_city)
					 ->set('vote', $vote)
					 ->where('id_from', $from)
					 ->where('id_to', $to)
					 ->update($this->table);
					 
			$res = $results[0];
			return $this->recalc( $to, $res->id_city, $res->vote, $id_city, $vote );
			
		}
	}
	
	private function cityScore( $inScore )
	{
		if( $inScore == 0 )
			return 0;
		if( $inScore > 0 )
			return 1;
		if( $inScore < 0 )
			return -1;
	}
	
	private function recalc( $to, $old_city, $old_vote, $new_city, $new_vote ) 
	{

		$oc_old_final = 0;
		$oc_new_final = 0;
		
		// TODO : make a function for city scores updates
			
		if( $old_city > 0 )	// update old city
		{
			$oc_old_score_res = $this->db->select( 'city_score' )
								 ->from( $this->incity_table )
								 ->where( 'id_city', $old_city )
								 ->where( 'id_citizen', $to )
								 ->get()
								 ->result();
			
			$oc_old_score = $oc_old_score_res[0]->city_score;
			
			$oc_new_score = $oc_old_score - $old_vote;
			$oc_old_final = $this->cityScore( $oc_old_score );
			$oc_new_final = $this->cityScore( $oc_new_score );
			
			$this->db->set( 'city_score', $oc_new_score )
					 ->where( 'id_citizen', $to )
					 ->where( 'id_city', $old_city )
					 ->update( $this->incity_table );
		}
		
		// update new city
		$nc_old_score_res = $this->db->select( 'city_score' )
							 ->from( $this->incity_table )
							 ->where( 'id_city', $new_city )
							 ->where( 'id_citizen', $to )
							 ->get()
							 ->result();
		
		$nc_old_score = $nc_old_score_res[0]->city_score;
		
		$nc_new_score = $nc_old_score + $new_vote;
		$nc_old_final = $this->cityScore( $nc_old_score );
		$nc_new_final = $this->cityScore( $nc_new_score );
		
		$this->db->set( 'city_score', $nc_new_score )
				 ->where( 'id_citizen', $to )
				 ->where( 'id_city', $new_city )
				 ->update( $this->incity_table );
		
		// update citizen score
		$results = $this->db->select( 'vote_pos, vote_neg, score' )
						->from( $this->citizen_table )
						->where ('id', $to )
						->get()
						->result();
		
		$res = $results[0];
		$vote_pos = $res->vote_pos;
		$vote_neg = $res->vote_neg;
		$score = $res->score;
		
		if( $oc_old_final > 0 )
			$vote_pos -= $oc_old_final;
		elseif( $oc_old_final < 0 )
			$vote_neg -= $oc_old_final;
		
		if( $nc_old_final > 0 )
			$vote_pos -= $nc_old_final;
		elseif( $nc_old_final < 0 )
			$vote_neg -= $nc_old_final;
		
		
		if( $oc_new_final > 0 )
			$vote_pos += $oc_new_final;
		elseif( $oc_new_final < 0 )
			$vote_neg += $oc_new_final;
		
		if( $nc_new_final > 0 )
			$vote_pos += $nc_new_final;
		elseif( $nc_new_final < 0 )
			$vote_neg += $nc_new_final;
		
		$score = $vote_pos + $vote_neg;
		
		$this->db->set( 'vote_pos', $vote_pos )
				 ->set( 'vote_neg', $vote_neg )
				 ->set( 'score', $score )
				 ->where( 'id', $to )
				 ->update( $this->citizen_table );
		
		return true;
	}
	
}


/* End of file vote.php */
/* Location: ./application/models/vote.php */